#!/usr/bin/env node

const {
  checkConfigFile,
  createConfigFile,
  readConfigFile,
  checkYhDesign,
  checkInstallPath,
} = require('./scripts');

function main() {
  checkYhDesign()
    // Promise.resolve()
    .then(
      () => checkConfigFile(),
      (e) => {
        console.log('checkConfigFile fail', e);
      }
    )
    .then(
      (b) => {
        const { code } = b;
        if (code === 2) {
          // 没有config文件，需要手动建立
          return checkInstallPath().then((where) =>
            createConfigFile([], { where })
          );
        }
        // 已有config文件，直接读入componentType
        return readConfigFile();
      },
      (e) => {
        console.log('createConfigFile| readConfigFile e', e);
      }
    )
    .then((e) => {
      global.log.success('yhpage has been initialized!');
      return 1;
    })
    .catch((e) => {
      global.log.error(e);
    });
}

// main();
module.exports = main;
